﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Albon.Model;
using Commonlib;
namespace AlbonWeb.Account
{
    public partial class Register : System.Web.UI.Page
    {
        protected readonly BLL.CookieBLL cookiebll = new BLL.CookieBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["id"].ToString() != "0")
                {
                    initData(Request["id"].ToString());
                }
                else
                {
                    userName.Value = Request["loginname"].ToString();
                }
                
            }
        }

        //提交注册信息
        protected void submit_Click(object sender, EventArgs e)
        {
            CookieInfo cookie = new CookieInfo();
            cookie.ID = int.Parse(Request["id"].ToString());
            cookie.UserName = userName.Value;
            cookie.Computername = computerName.Value.Trim();
            cookie.regName = firstName.Value.Trim() + '.' + lastName.Value.Trim();
            cookie.Email = regEmail.Value.Trim();
            cookie.Browser = Request.Browser.Type;
            cookie.Passed = true;
            cookie.CreateDate = DateTime.Now;
            string cookievalue = cookie.Computername + "$" + cookie.regName + "$" + cookie.Email + "$"
                + cookie.Browser + "$" + cookie.CreateDate.ToString();

            //Cookie有效期一年，超过一年要重新注册
            CookieHelper.AddCookies(cookie.UserName, cookievalue,DateTime.Now.AddYears(1));
            cookiebll.alterCookie(cookie);
            JavaScriptHelper.AlertAndRedirect("Success！We will process your request within 24 hours", "/Default.html");
        }
        //初始化要修改的信息
        protected void initData(string id)
        {
            string cmdstr = "select * from CookieInfo where id="+id;
            DataTable dt=SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString,CommandType.Text, cmdstr, null).Tables[0];
            userName.Value = dt.Rows[0]["userName"].ToString();
            computerName.Focus();
            computerName.Value = dt.Rows[0]["computerName"].ToString();
            string[] regName = dt.Rows[0]["regName"].ToString().Split('.');
            firstName.Value=regName[0];
            lastName.Value = regName[1];
            regEmail.Value = dt.Rows[0]["email"].ToString();
        }
    }
}